<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="评论人" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入评论人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="discussList"
      row-key="id"
      default-expand-all
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column
        label="事件类型"
        align="center"
        prop="contentType"
        :formatter="contentTypeFormat"
      />
      <el-table-column label="内容" align="center" prop="content" />
      <el-table-column
        label="发布头像"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <img
            :src="scope.row.avatar"
            alt=""
            style="width: 50px; height: 50px; border-radius: 50px"
          />
        </template>
      </el-table-column>
      <el-table-column label="发布人" align="center" prop="nickName" />
      <el-table-column label="图片" align="center" class-name="small-padding fixed-width" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="ImagePreview(scope.row)"
          >图片预览</el-button
          >
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户评论信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="评论id" prop="parentId">
          <treeselect v-model="form.parentId" :options="discussOptions" :normalizer="normalizer" placeholder="请选择评论id" />
        </el-form-item>
        <el-form-item label="评论内容" prop="content">
          <el-input
            v-model="form.content"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="发布人" prop="userId">
          <el-select
            v-model="form.userId"
            placeholder="请选择发布人"
            :disabled="opendis"
            filterable
            clearable
          >
            <el-option
              v-for="dict in userOptions"
              :key="dict.userId"
              :label="dict.nickName"
              :value="parseInt(dict.userId)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="上传图片">
          <imageUpload v-model="form.imgUrl" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 图片预览-->
    <el-dialog :title="title" :visible.sync="imgopen" width="700px" append-to-body>
      <ImagePreview v-model="form.imgUrl" />
    </el-dialog>
  </div>
</template>

<script>
import { listDiscuss, getDiscuss, delDiscuss, addDiscuss, updateDiscuss, exportDiscuss } from "@/api/interest/discuss";
import { getUserList } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Discuss",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 禁用用户下拉框选择
      opendis: true,
      //图片预览弹出层
      imgopen: false,
      // 总条数
      total: 0,
      // 用户评论信息表格数据
      discussList: [],
      // 用户评论信息树选项
      discussOptions: [],
      // 内容类型字典
      contentTypeOptions: [],
      //用户信息列表
      userOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        content: null,
        parentId: null,
        reauserId: null,
        userId: null,
        imgUrl: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.queryParams.reauserId = this.$route.params && this.$route.params.reauserId;
    this.getList();
    this.getUser();
    this.getDicts("hxc_content_type").then((response) => {
      this.contentTypeOptions = response.data;
    });
  },
  methods: {
    //获取账户信息列表
    getUser() {
      getUserList().then((response) => {
        this.userOptions = response.data;
      });
    },
    /** 查询用户评论信息列表 */
    getList() {
      this.loading = true;
      listDiscuss(this.queryParams).then(response => {
        this.discussList = this.handleTree(response.data, "id", "parentId");
        this.loading = false;
      });
    },
    /** 转换用户评论信息数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.content,
        children: node.children
      };
    },
	  /** 查询用户评论信息下拉树结构 */
    getTreeselect() {
      listDiscuss().then(response => {
        this.discussOptions = [];
        const data = { id: 0, content: '顶级节点', children: [] };
        data.children = this.handleTree(response.data, "id", "parentId");
        this.discussOptions.push(data);
      });
    },
    // 内容类型字典翻译
    contentTypeFormat(row, column) {
      return this.selectDictLabel(this.contentTypeOptions, row.contentType);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        content: null,
        reauserId: null,
        userId: null,
        imgUrl: null,
        createTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
     /** 图片预览按钮操作 */
    ImagePreview(row) {
      this.reset();
      this.form.imgUrl = row.imgUrl;
      this.imgopen = true;
      this.title = "图片预览";
      this.opendis = false;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.getTreeselect();
      //赋值中间表主键
      this.form.reauserId = this.queryParams.reauserId;
      this.open = true;
      this.title = "添加用户评论信息";
      this.opendis = false;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      const id = row.id || this.ids;
      getDiscuss(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改用户评论信息";
        this.opendis = true;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateDiscuss(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDiscuss(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm(
        '是否确认删除用户评论信息编号为"' + ids + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delDiscuss(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有用户评论信息数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.exportLoading = true;
          return exportDiscuss(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
          this.exportLoading = false;
        })
        .catch(() => {});
    },
  },
};
</script>
